package com.score.student.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.score.student.Expections.AccountNotFoundException;
import com.score.student.Expections.PasswordErrorException;
import com.score.student.constant.JwtClaimsConstant;
import com.score.student.constant.MessageConstant;
import com.score.student.domain.dto.UserAddDTO;
import com.score.student.domain.dto.UserLoginDTO;
import com.score.student.domain.dto.UserPageDTO;
import com.score.student.domain.dto.UserUpdateDTO;
import com.score.student.domain.po.User;
import com.score.student.domain.vo.UserLoginVO;
import com.score.student.domain.vo.UserVO;
import com.score.student.mapper.UserMapper;
import com.score.student.properties.JwtProperties;
import com.score.student.service.IUserService;
import com.score.student.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final JwtProperties jwtProperties;

    @Override
    public UserLoginVO login(UserLoginDTO userLoginDTO) {

        String account = userLoginDTO.getAccount();
        String password = DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes());
        Integer identify = userLoginDTO.getIdentify();

        User user = lambdaQuery()
                .eq(User::getAccount, account)
                .eq(User::getIdentify, identify)
                .one();
        //判断用户是否存在
        if (user == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        String TPassword = user.getPassword();
        //判断密码是否正确
        if(!password.equals(TPassword)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setId(user.getId());
        userLoginVO.setIdentify(user.getIdentify());
        userLoginVO.setName(user.getName());
        userLoginVO.setToken(token);
        userLoginVO.setName(user.getName());
        userLoginVO.setImage(user.getImage());
        return userLoginVO;
    }

    @Override
    public UserVO queryAllStudent(UserPageDTO userPageDTO) {
        String message = userPageDTO.getMessage();

        List<User> users = lambdaQuery()
                .like(message != null, User::getName, message)
                .orderBy(true,true,User::getAccount)
                .list();

        UserVO userVO = new UserVO();
        userVO.setUsers(users);
        userVO.setTotal(users.size());

        return userVO;
    }

    @Override
    public void add(UserAddDTO userAddDTO) {
        userAddDTO.setPassword(DigestUtils.md5DigestAsHex(userAddDTO.getPassword().getBytes()));
        User user = BeanUtil.copyProperties(userAddDTO, User.class);
        this.save(user);
    }

    @Override
    public void updateByUserId(UserUpdateDTO userUpdateDTO) {
        userUpdateDTO.setPassword(DigestUtils.md5DigestAsHex(userUpdateDTO.getPassword().getBytes()));
        User user = BeanUtil.copyProperties(userUpdateDTO, User.class);
        this.updateById(user);
    }


}
